
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Security;
using Swaf.Container;

namespace Forums.Gui
{
	public class ForumBizProc : Articles.Gui.ArticleManagementBizProc
	{
		public ForumBizProc(ActiveBizProc procState) : base(procState){}

		public override bool CanEdit
		{
			get
			{
				return base.CanEdit || State.InProgressAddBizObj != null;
			}
		}

		protected override Privileges CurrentEditPrivilege
		{
			get
			{
				Privileges priv = CurrentPrincipal.getPrivilege("AppFunctions.ManageForumGroups");
				if (m_currentBizObj != null)
				{
					int depth = (int)m_currentBizObj.get("depth");
					if(depth >= 2)
						priv = CurrentPrincipal.getPrivilege("AppFunctions.ManageForumPosts");
				}
				if (priv < Privileges.Edit && State.InProgressAddBizObj != null)
					priv = Privileges.Edit;

				return priv;
			}
		}

		protected override IBizObj createNewBizObj()
		{
			IBizObj f = base.createNewBizObj();
			f.put("treeName", "Forum");
			object pid = m_procState.get("pid");
			IResults rc = App.ruleMgr.execute("getArticleDetail", pid, false, DetailLevel.idAndTitle, false);
			IBizObj parent = rc.CallResults as IBizObj;
			if (parent != null)
			{
				f.put("parentId", parent.get("id"));
				int parentDepth = (int)parent.get("depth");
				if (parentDepth == 3)
					f.put("baseTitle", "Re: " + parent.get("baseTitle"));
				else
					f.put("baseTitle", "Parent Depth: " + parentDepth);
			}
			else
			{
				f.put("baseTitle", "");
				f.putValidated("parentId", m_procState.get("pid"));
			}
			return f;
		}

		protected override void getBizObjList()
		{
			base.getBizObjList();
			if (m_bizobjs != null && (m_bizobjs.numElements == 0 || !m_bizobjs[m_bizobjs.numElements - 1].isNew))
				m_bizobjs.Add(createNewBizObj());
		}

		protected override void setupGetArticleListMacros(FlexiMap m)
		{
			m.put("objectTreeName", "Forum");
		}

		public override void updateActionState()
		{
			base.updateActionState();
			IBizObj o = CurrentBizObj;
			if (o == null)
				o = m_procState.get("parent") as IBizObj;
			
			if(o != null && !CanEdit)
			{
			int depth = (int)o.get("depth");
			if (depth >= 2 && CurrentPrincipal.getPrivilege("AppFunctions.CreateForumPosts") >= Privileges.Edit)
				((GenericAction)Actions["AddBizObj"]).Availability = ActionAvailability.enabled;
			}

		}
	}
}
